# -*- coding: utf-8 -*-

from django.template.response import TemplateResponse
from django.contrib.auth.decorators import login_required
from django.forms.models import modelformset_factory
from models import Commodity,PR_STATUS_CHOICES,PurchaseOrderDetail,Project
from django.shortcuts import render_to_response,render
from django.template import RequestContext

from django.http import HttpResponseRedirect, Http404
import math,logging,string
from django.views.decorators.cache import cache_page
 
@login_required()#login_url='/'
def home(request):
    return TemplateResponse(request,'warehouse/index.html',{'app':'warehouse'}
                            )


@login_required()#login_url='/'
def project(request):       
    pr_choice = list(PR_STATUS_CHOICES)
    pr_choice = [x for x,_ in pr_choice]
    page=1
    if request.method == 'POST':
        pr_choice = request.POST.getlist('PR_STATUS_CHOICES')
    else:
        pr_choice = list( request.GET.get('status',string.join(pr_choice,''))) 
        page = request.GET.get('p','')
        if page:
            page= int(page)
        else:
            page=1
    
    #全部
    items = Project.objects.all()
    
    pagesize = 10
    show_pager = 1
    record_count = items.count()
    page_count = str(int(math.ceil(float(record_count)/float(pagesize))))
    first_this_page = (int(page)-1)*pagesize
    last_this_page = min(record_count,int(page)*pagesize)
    
    queryparam = {
                          'query':'?status='+string.join(pr_choice,'')+'&',
                          'qpage':page,                           
                          'record_count':record_count,
                          'pagesize':pagesize,
                          'pagecount':page_count,
                          'first_this_page':first_this_page+1,
                          'last_this_page':last_this_page,
                          #'cache_key':cache_key,
                          #'cache_key_no_page':cache_key_no_page,
                          }  
    info = items[first_this_page :last_this_page]
   
    
    return TemplateResponse(request,'warehouse/project/list.html',
                            {'app':'project',
                            'items':info,
                            'queryparam':queryparam,
                            'show_pager':show_pager,
                            'PR_STATUS_CHOICES':PR_STATUS_CHOICES,
                            'PR_STATUS_CHOICES_POST':pr_choice,
                            'current_url':'/erp/project/',
                            'base_url':'/erp',
                            'title':'项目',
                             }
                            )
    
@login_required()#login_url='/'
def commodity(request):       
    pr_choice = list(PR_STATUS_CHOICES)
    pr_choice = [x for x,_ in pr_choice]
    page=1
    if request.method == 'POST':
        pr_choice = request.POST.getlist('PR_STATUS_CHOICES')
    else:
        pr_choice = list( request.GET.get('status',string.join(pr_choice,''))) 
        page = request.GET.get('p','')
        if page:
            page= int(page)
        else:
            page=1
    
    #全部
    items = Commodity.objects.all()
    
    pagesize = 10
    show_pager = 1
    record_count = items.count()
    page_count = str(int(math.ceil(float(record_count)/float(pagesize))))
    first_this_page = (int(page)-1)*pagesize
    last_this_page = min(record_count,int(page)*pagesize)
    
    queryparam = {
                          'query':'?status='+string.join(pr_choice,'')+'&',
                          'qpage':page,                           
                          'record_count':record_count,
                          'pagesize':pagesize,
                          'pagecount':page_count,
                          'first_this_page':first_this_page+1,
                          'last_this_page':last_this_page,
                          #'cache_key':cache_key,
                          #'cache_key_no_page':cache_key_no_page,
                          }  
    info = items[first_this_page :last_this_page]
   
    
    return TemplateResponse(request,'warehouse/commodity/list.html',
                            {'app':'commodity',
                            'commodity':info,
                            'queryparam':queryparam,
                            'show_pager':show_pager,
                            'PR_STATUS_CHOICES':PR_STATUS_CHOICES,
                            'PR_STATUS_CHOICES_POST':pr_choice,
                            'current_url':'/erp/commodity/',
                            'base_url':'/erp',
                            'title':'物品',
                             }
                            )

@login_required()#login_url='/'
def commodity_buyhistory(request,id):   
    
    #pr_choice = list(PR_STATUS_CHOICES)
    #pr_choice = [x for x,_ in pr_choice]
    page=1
    sort='0'
    if request.method == 'POST':
        #pr_choice = request.POST.getlist('PR_STATUS_CHOICES')
        pass
    else:
        #pr_choice = list( request.GET.get('status',string.join(pr_choice,''))) 
        page = request.GET.get('p','')
        sort = request.GET.get('s','0')
        if page:
            page= int(page)
        else:
            page=1
    
    #全部
    items = PurchaseOrderDetail.objects.filter(POD_Commodity__id=id)
    items = items.filter(POD_PurchaseOrder__PO_Ststus='F')
    
    if sort=='0':
        items = items.order_by('-POD_PurchaseOrder__PO_LastModify_Date')
    elif sort=='1':
        items = items.order_by('-POD_Price')
    elif sort=='2':
        items = items.order_by('POD_Price')
    
    pagesize = 2
    show_pager = 1
    record_count = items.count()
    page_count = str(int(math.ceil(float(record_count)/float(pagesize))))
    first_this_page = (int(page)-1)*pagesize
    last_this_page = min(record_count,int(page)*pagesize)
    
    queryparam = {
                    'query':'?s='+sort+'&',
                          #'query':'?status='+string.join(pr_choice,'')+'&',
                          'qpage':page,                           
                          'record_count':record_count,
                          'pagesize':pagesize,
                          'pagecount':page_count,
                          'first_this_page':first_this_page+1,
                          'last_this_page':last_this_page,
                          #'cache_key':cache_key,
                          #'cache_key_no_page':cache_key_no_page,
                          }  
    items = items[first_this_page :last_this_page]   
    
    return TemplateResponse(request,'warehouse/commodity/buyhistory.html',
                            {'app':'commodity',
                            'items':items,
                            'queryparam':queryparam,
                            'show_pager':show_pager,
                            'PR_STATUS_CHOICES':PR_STATUS_CHOICES,
                            #'PR_STATUS_CHOICES_POST':pr_choice,
                            'current_url':'/erp/commodity/'+id+'/buyhistory/',
                            'back_url':'/erp/commodity/',
                            'sort':sort,
                             }
                            )
 